var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const session = require("express-session");



var indexRouter = require('./routes/index');
var navsRouter = require('./routes/navs');

var apiNavs = require('./api/navs');
var apiPs = require('./api/ps');
var apiPc = require('./api/pc');
var apiSchool = require('./api/school');
var apiHotP = require('./api/hotP');
var apiHL = require('./api/HL');
var apiEcd = require('./api/ecd');
var apiCI = require('./api/ci');
var apiCls = require('./api/cls');
var apiAC = require('./api/ac');
var apiBI = require('./api/bi');
var apiTA = require('./api/ta');
var apiTF = require('./api/tf');
var apiTH = require('./api/th');
var apiTHot = require('./api/thot');
var apiTR = require('./api/tr');
var apiCity = require('./api/city');
var apiHC = require('./api/hc');
var apiInh = require('./api/inh');
var apiUser = require('./api/user');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({
  secret: '千锋H5，牛！',   // 加密信息，可以随便写
  resave: false,      // 强制保存session，默认为true，建议设置为false
  saveUninitialized: true,    // 强制将未初始化的session存储，默认为true，建议为true
  cookie: { maxAge: 1000 * 20 * 60 }    // 过期时间，毫秒数
}))


app.use('/api/navs', apiNavs);
app.use('/api/ps', apiPs);
app.use('/api/pc', apiPc);
app.use('/api/school', apiSchool);
app.use('/api/hotP', apiHotP);
app.use('/api/HL', apiHL);
app.use('/api/ecd', apiEcd);
app.use('/api/ci', apiCI);
app.use('/api/cls', apiCls);
app.use('/api/bi', apiBI);
app.use('/api/ac', apiAC);
app.use('/api/ta', apiTA);
app.use('/api/tf', apiTF);
app.use('/api/th', apiTH);
app.use('/api/thot', apiTHot);
app.use('/api/tr', apiTR);
app.use('/api/city', apiCity);
app.use('/api/hc', apiHC);
app.use('/api/inh', apiInh);
app.use('/api/user', apiUser);

app.use('/', indexRouter);
app.use('/navs', navsRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
